<template>
  <div class="login">
    <div class="login-containers" @click.stop="">
      <!-- <i class="el-icon-close loginClose" @click="close"></i> -->
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">
          <!-- {{ systitle }} -->
        </h3>
        <el-form-item prop="username" class="loginFormItem">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="loginFormItem">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" class="loginFormItem">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;" class="loginFormItem-checkbox">记住密码</el-checkbox>
        <el-form-item style="width:100%;" class="loginFormItem">
          <el-button :loading="loading" size="medium" style="width:100%;" class="loginFormItem-btn"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
          <!-- <div style="margin-top: 12px">
            <el-button type="text" size="mini">
              <a href="https://www.bear20.com/window/4211/472030921.html?f=bdj_695506" target="_black">推荐浏览器下载:chrome浏览器
                <span>下载</span></a>
            </el-button>


          </div> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script>
import { getCodeImg } from "@/api/login";
import {getToken} from "@/utils/auth";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      systitle: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    close() {
      this.$emit("close")
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set("passwordNum", this.loginForm.password, { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
            Cookies.remove("passwordNum");
          }
          this.$store.dispatch("Login", this.loginForm).then((res,result) => {
         
            console.log(getToken())
            if (getToken()&&getToken()!=undefined&&getToken()!=''&&getToken()!=null&&getToken()!='undefined') {
              // 判断当前用户是否已拉取完user_info信息
              this.$store.dispatch('GetInfo').then(() => {

                this.$store.dispatch('GenerateRoutes').then(accessRoutes => {
                  // 根据roles权限生成可访问的路由表
                  this.$router.addRoutes(accessRoutes) // 动态添加可访问路由表
                  this.loading = false
                  this.$emit("loginSucess")
                })
              }).catch(err => {
                this.loading = false
                this.$store.dispatch('LogOut').then(() => {
                  //this.$Message.error(err)
                  this.loading = false
                  //this.$router.push({ path: "/" }).catch(()=>{});
                })
              })
            }else{
              this.loading = false;
            }


          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>
  
<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

}

.loginClose {
  position: absolute;
  top: 5px;
  right: 10px;
  font-size: 24px;
  color: #999;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.loginFormItem{
  margin-bottom:40px;
}
.login-containers {
  position: relative;
}
.el-input--prefix .el-input__inner{
  padding-left:60px;
}
.loginFormItem-checkbox{
  font-size:16px;
  color:#D3946A;
}
.el-checkbox__inner:hover{
  border-color:#D3946A;
}
.el-checkbox__input.is-checked + .el-checkbox__label{
  color:#D3946A;
  font-size:16px;
}
.el-checkbox__input.is-focus .el-checkbox__inner{
  border-color:#D3946A;
}
.el-checkbox__inner{
  width:16px;
  height:16px;
}
.el-checkbox__label{
  font-size:16px;
}
.el-checkbox__input.is-checked .el-checkbox__inner{
  color:#D3946A;
  border-color:#D3946A;
  background-color:#D3946A;
  font-size:14px;
}
.loginFormItem-btn{
  height:60px;
  border-radius:60px;
  font-size:20px;
  border:none;
box-shadow: 0px 4px 10px 0px rgba(206,139,102,0.5);
background: linear-gradient(270deg, #CC8764 0%, #DAA170 100%);
color:#fff;
}
.loginFormItem-btn:hover,.loginFormItem-btn:focus{
  color:#fff;
  border:none;
}
.login-form {
  border-radius: 20px;
  background: #E1E2E5;
  width:480px;
  padding: 60px 25px 60px 25px;
  box-shadow: 40px 40px 50px 0px rgba(0,0,0,0.05);
  .el-input {
    height:60px;
   
    input {
      height:60px;
      border-radius:15px;
      font-size:20px;
    }
  }

  .input-icon {
    height: 60px;
    width: 28px;
    margin-left:15px;
  }
}

.login-tip {
  font-size: 14px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
  